<?php
class Custom_Search_Lucene_Document extends Zend_Search_Lucene_Document {
    
    const DOCREF = 'docRef';
    const CLASS_NAME = 'class';
    const KEY = 'key';
    const ISBN = 'ISBN';
    const NAME = 'name';
    const DESCRIPTION = 'description';
    const AUTHOR = 'author';
    const PUBLICATION_DATE = 'publication_date';
    const BINDING = 'binding';
    const ASIN = 'ASIN';
    const SMALL_IMAGE = 'small_image';
    const MEDIUM_IMAGE = 'medium_image';
    const LARGE_IMAGE = 'large_image';
    const HOST = 'host';
    const CITIES = 'cities';
    
    /**
     * Constructor of book lucene document.
     * 
     * @param $class
     * @param $id
     * @param $book
     */
    public function __construct($class, $id, $book) {
        $this->addField(Zend_Search_Lucene_Field::unStored(self::DOCREF, $class.':'.$id, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::CLASS_NAME, $class, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::KEY, $id, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::Keyword(self::ISBN, $book->ISBN, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::Text(self::NAME, $book->name, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::DESCRIPTION, $book->description, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::Text(self::AUTHOR, $book->author, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::PUBLICATION_DATE, $book->publication_date, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::BINDING, $book->binding, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::ASIN, $book->ASIN, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::SMALL_IMAGE, $book->small_image, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::MEDIUM_IMAGE, $book->medium_image, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::LARGE_IMAGE, $book->large_image, 'UTF-8'));
        $this->addField(Zend_Search_Lucene_Field::UnIndexed(self::CITIES, $this->getBookCities($book), 'UTF-8'));
    }
    
    /**
     * Get city which given book have.
     * 
     * @param $book
     * @return return city string, separated by comma
     */
    private function getBookCities($book) {
    	$users = Book::getUser($book, null);
    	$ret = array();
    	foreach ($users as $user) {
    		$city = User::getTopCity($user);
    		$ret[$city->id] = $city->id;
    	}
    	return implode(",", $ret);
    }
}
?>